package com.hydra.baseui.viewpager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;

import com.hydra.baseui.R;

/**
 * Created by Hydra on 2017/7/30.
 *
 */
public class GViewPager extends ViewPager {

    private boolean mEnableTouchScroll;
    private boolean mEnableSmoothScroll;

    public GViewPager(Context context) {
        super(context);

        init(null);
    }

    public GViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if(attrs == null) {
            mEnableTouchScroll = true;
            mEnableSmoothScroll = true;
        } else {
            TypedArray array = getContext().obtainStyledAttributes(attrs,
                    R.styleable.GViewPager);

            mEnableTouchScroll = array.getBoolean(R.styleable.GViewPager_enableTouchScroll, true);
            mEnableSmoothScroll = array.getBoolean(R.styleable.GViewPager_enableSmootScroll, true);

            array.recycle();
        }
    }

    public void setEnableTouchScroll(boolean enableTouchScroll) {
        mEnableTouchScroll = enableTouchScroll;
    }

    public void setEnableSmoothScroll(boolean canSmoothScroll) {
        mEnableSmoothScroll = canSmoothScroll;
    }

    public boolean isEnableTouchScroll() {
        return mEnableTouchScroll;
    }

    public boolean isEnableSmoothScroll() {
        return mEnableSmoothScroll;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (!mEnableTouchScroll) {
            return false;
        }

        return super.onInterceptTouchEvent(arg0);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!mEnableTouchScroll) {
            return false;
        }

        return super.onTouchEvent(event);
    }

    @Override
    public void setCurrentItem(int item) {
        if(!mEnableSmoothScroll) {
            super.setCurrentItem(item, false);
        } else {
            super.setCurrentItem(item);
        }
    }

    public void release() {
        clearOnPageChangeListeners();
    }
}
